<?php
    /*
    * Copyright (c) 2007, Osuvaldo Ramos
    * 
    * All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    *
    *    * Redistributions of source code must retain the above copyright
    *      notice, this list of conditions and the following disclaimer.
    *    * Redistributions in binary form must reproduce the above copyright
    *      notice, this list of conditions and the following disclaimer in the
    *      documentation and/or other materials provided with the distribution.
    *    * Neither the name of the CoreTech nor the names of its
    *      contributors may be used to endorse or promote products derived from
    *      this software without specific prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * 
    * $Id: Diplomacy.php 2 2008-01-21 02:48:28Z xdracox $
    */
    
    if ( !defined('BNETCS') || !defined('ROOT') ) exit();
    
    include_once ROOT.'/mods/RankSystem/'.$CONF['mods']['ranksystem']['name']
        .'.php';
    
    class Diplomacy
    {
        var $smarty;
        
        function Diplomacy()
        {
            $this->smarty = new Smarty();
            $this->smarty->template_dir = ROOT.'/mods/Diplomacy';
            $this->smarty->compile_dir = $this->smarty->template_dir;
        }
        
        function onLogin()
        {
        }
        
        function displayConsole()
        {
            global $auth;
            $mid = $auth->getAuthData('member_id');
            
            $actions = array();
            
            if ( RankSystem::getRank($mid) >= RANK_BRIGADIER_GENERAL )
            {
                $actions[] = array
                (
                    'text' => 'Add Clan',
                    'addr' => 'console.php?a=m&amp;m=Diplomacy&amp;p=ac'
                );
                
                $actions[] = array
                (
                    'text' => 'Remove Clan',
                    'addr' => 'console.php?a=m&amp;m=Diplomacy&amp;p=dc'
                );
                
                $actions[] = array
                (
                    'text' => 'Change Clan Status',
                    'addr' => 'console.php?a=m&amp;m=Diplomacy&amp;p=cs'
                );
            }
            
            if ( sizeof($actions) % 2 == 1 )
            {
                $actions[] = array
                (
                    'text' => '',
                    'addr' => '#'
                );
            }
            
            $this->smarty->assign('actions', $actions);
            $this->smarty->display('console.tpl');
        }
        
        function displayDiplomacyPage()
        {
            global $mdb2;
            
            $sql = 'SELECT *
                FROM bnetcs_diplomacy';
            $result = $mdb2->query($sql);
            $clans = array();
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_ERROR);
            }
            else
            {
                while ( $row = $result->fetchRow() )
                {
                    $clans[] = array
                    (
                        'name' => $row['diplo_clan_name'],
                        'leader' => $row['diplo_clan_leader'],
                        'status' => $row['diplo_clan_status'],
                        'website' => $row['diplo_clan_website']
                    );
                }
            }
            
            $this->smarty->assign('clans', $clans);
            $this->smarty->display('diplomacy.tpl');
        }
        
        function displayPage($page)
        {
            global $auth, $mdb2;
            
            $rs = new RankSystem();
            $mid = $auth->getAuthData('member_id');
            
            if ( $rs->getRank($mid) < RANK_BRIGADIER_GENERAL )
            {
                $msg = 'You cannot modify diplomacy settings.';
                $_SESSION['errors'] = array($msg);
                header('Location: console.php');
                return;
            }
            
            switch ( $page )
            {
                case 'ac':
                {
                    
                    if ( isset($_POST['clan_name']) && isset($_POST['status'])
                        && isset($_POST['clan_leader'])
                        && isset($_POST['website']) )
                    {
                        $clan_name = htmlentities($_POST['clan_name']);
                        $clan_leader = htmlentities($_POST['clan_leader']);
                        $status = htmlentities($_POST['status']);
                        $website = htmlentities($_POST['website']);
                        
                        $sql = sprintf('INSERT
                            INTO bnetcs_diplomacy (diplo_clan_name,
                                diplo_clan_leader, diplo_clan_status,
                                diplo_clan_website)
                            VALUES ("%s", "%s", "%s", "%s")', $clan_name,
                            $clan_leader, $status, $website);
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'Diplomacy settings succesfully modified.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        display_header();
                        $this->smarty->display('addclan.tpl');
                        display_footer();
                    }
                    
                    break;
                }
                case 'dc':
                {
                    if ( isset($_GET['c']) )
                    {
                        $clan_id = intval($_GET['c']);
                        
                        $sql = sprintf('DELETE FROM bnetcs_diplomacy
                            WHERE diplo_id = %d', $clan_id);
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'Successfully removed clan from diplomacy.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        $clans = array();
                        
                        $sql = 'SELECT *
                            FROM bnetcs_diplomacy';
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            while ( $row = $result->fetchRow() )
                            {
                                $clans[] = array
                                (
                                    'id' => $row['diplo_id'],
                                    'name' => $row['diplo_clan_name']
                                );
                            }
                            
                            $this->smarty->assign('clans', $clans);
                            
                            display_header();
                            $this->smarty->display('remclan.tpl');
                            display_footer();
                        }
                    }
                    
                    break;
                }
                
                case 'cs':
                {
                    if ( isset($_POST['clan_id'])
                        && isset($_POST['new_status']) )
                    {
                        $clan_id = intval($_POST['clan_id']);
                        $status = htmlentities($_POST['new_status']);
                        
                        $sql = sprintf('UPDATE bnetcs_diplomacy
                            SET diplo_clan_status = "%s"
                            WHERE diplo_id = %d', $status, $clan_id);
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'Successfully changed clan diplomacy status.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        $clans = array();
                        
                        $sql = 'SELECT *
                            FROM bnetcs_diplomacy';
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            while ( $row = $result->fetchRow() )
                            {
                                $clans[] = array
                                (
                                    'id' => $row['diplo_id'],
                                    'name' => $row['diplo_clan_name'],
                                    'status' => $row['diplo_clan_status']
                                );
                            }
                            
                            $this->smarty->assign('clans', $clans);
                            
                            display_header();
                            $this->smarty->display('changestatus.tpl');
                            display_footer();
                        }
                    }
                    
                    break;
                }
            }
        }
    }
?>
